package com.ruoyi.mes.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.mes.vo.qualityinspection.QualityInspectionVo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 质检工具类
 */
public class QualityInspectionUtils {
    /**
     * 开始质检
     * @return
     */
    public static QualityInspectionVo qualityInspection() throws IOException {
        // API地址
        String url = "http://192.168.1.100:5000/mes/robot-arm/4axis";
        // 请求数据
        String requestData = "{\"action\":\"auto_move_and_detect\"}";
        QualityInspectionVo qualityInspectionVo = new QualityInspectionVo();
        try {
            // 创建URL对象
            URL apiUrl = new URL(url);
            // 打开URL连接
            HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
            // 设置请求方法为POST
            connection.setRequestMethod("POST");
            // 设置请求头部信息
            connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            // 允许输出流写入数据
            connection.setDoOutput(true);
            // 将请求数据写入输出流
            connection.getOutputStream().write(requestData.getBytes("UTF-8"));
            // 发送HTTP请求
            connection.connect();

            // 读取响应数据
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // 关闭连接
            connection.disconnect();

            // 解析JSON数据并存储在对象中
            String responseData = response.toString();
            JSONObject json = JSONObject.parseObject(responseData);
            qualityInspectionVo = JSON.parseObject(responseData, QualityInspectionVo.class);


        } catch (Exception e) {
            e.printStackTrace();
        }

        return qualityInspectionVo;
    }
}

